package com.wish.g04.financial.controller;

import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.Jeepay;
import com.jeequan.jeepay.JeepayClient;
import com.jeequan.jeepay.exception.JeepayException;
import com.jeequan.jeepay.model.PayOrderCreateReqModel;
import com.jeequan.jeepay.request.PayOrderCreateRequest;
import com.jeequan.jeepay.response.PayOrderCreateResponse;
import com.wish.g04.common.core.utils.ip.IpUtils;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.security.annotation.PreAuthorize;
import com.wish.g04.financial.config.PayInfoConfig;
import com.wish.g04.financial.dto.PayDTO;
import com.wish.g04.financial.service.IPayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;


/**
 * @Author Z-7
 * @Date 2022/6/6
 */
@Slf4j
@RestController
@RequestMapping("pay")
public class PayController {
    @Autowired
    private IPayService iPayService;

    @Autowired
    private PayInfoConfig payInfoConfig;

    @PostConstruct
    public void initJeepay() {
        Jeepay.setApiBase(payInfoConfig.getApiBase());
        Jeepay.apiKey = payInfoConfig.getApiKey();
        Jeepay.mchNo = payInfoConfig.getMchNo();
        Jeepay.appId = payInfoConfig.getAppId();
        System.out.println(" _______                  \n" +
                "|_   __ \\                 \n" +
                "  | |__) |,--.    _   __  \n" +
                "  |  ___/`'_\\ :  [ \\ [  ] \n" +
                " _| |_   // | |,  \\ '/ /  \n" +
                "|_____|  \\'-;__/[\\_:  /   \n" +
                "                 \\__.'    \n");
    }
    @PostMapping
    @PreAuthorize(hasPermi = "financing:pay:allow")
    public AjaxResult payTest(@RequestBody PayDTO payDTO) {
        PayOrderCreateResponse response = iPayService.startPay(payDTO);
        if (response != null) {
            return AjaxResult.success(response);
        }
        return AjaxResult.error();
    }
}
